package com.happyclub.lbj.service.impl;

import com.happyclub.lbjdao.ActivityPhotoAlbum;
import com.happyclub.lbjdao.SaveActivityUser;
import com.happyclub.lbjdao.SendActivity;
import com.happyclub.lbj.service.IActivityService;
import com.happyclub.lcy.service.ITeamService;
import com.happyclub.paul.dao.PhotoLibraryRepostitory;
import com.happyclub.paul.service.PersonalPhotoAlbumService;
import com.happyclub.paul.service.PhotoAlbumService;
import com.happyclub.pojo.*;
import com.happyclub.utils.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.transaction.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import static com.happyclub.errormsg.ErrorMsgProvider.CAN_NOT_FIND;
import static com.happyclub.errormsg.ErrorMsgProvider.SYSTEM_BUSY;

/**
 * @author 高欢
 * @description TODO
 * @date 2019/4/2
 */
@Service
public class ActivityServiceImpl implements IActivityService {

    @Autowired
    SendActivity sendActivity;

    @Autowired
    SaveActivityUser saveActivityUser;

    @Autowired
    ITeamService iTeam;//lcy

    @Autowired
    PhotoAlbumService photoAlbumService;//paul

    @Autowired
    PersonalPhotoAlbumService personalPhotoAlbumService;//paul

    @Autowired
    ActivityPhotoAlbum  IactivityPhotoAlbum;

    @Autowired
    PhotoLibraryRepostitory photoLibraryRepostitory;//paul

    @Transactional
    @Override
    public JsonResult insertActivity(TbActivity activity) {
        JsonResult jsonResult = new JsonResult();
        TbActivity save = sendActivity.save(activity);
        if (save==null){
            jsonResult.setMsg(SYSTEM_BUSY);
            jsonResult.setErrorCode("500");
            return jsonResult;
        }
        jsonResult.setResult(save);//创建活动时先执行插入活动操作,成功之后返回该活动ID 进行图片上传
        return jsonResult;
    }

    //调用lcy
    @Override
    public JsonResult findTeamByUser(String userId) {
        return iTeam.getAllTeamInforByUserId(userId);
    }

    //调用paul
    @Override
    public JsonResult findPhotoAlbumByUserId(String userId) {
        JsonResult<Object> jsonResult = new JsonResult<>();
        List<TbPersonalPhotoAlbum> tbPersonalPhotoAlbums = personalPhotoAlbumService.showUserPhotoAlbum(userId);
        if (tbPersonalPhotoAlbums.size()==0){
            jsonResult.setMsg("亲!您还没有相册");
            jsonResult.setErrorCode("500");
            return jsonResult;
        }
        for (TbPersonalPhotoAlbum ph:tbPersonalPhotoAlbums){
            TbPhotoAlbum tbPhotoAlbum = photoAlbumService.queryPhotoAlbumsById(ph.getPhotoAlbumId());
            ph.setPhotoAlbumName(tbPhotoAlbum.getAlbumName());
        }
        jsonResult.setResult(tbPersonalPhotoAlbums);
        return jsonResult;
    }

    @Transactional
    @Override
    public JsonResult imgUpLoad(String activityId,MultipartFile[] multipartFile, String userId) {
        JsonResult jsonResult = photoAlbumService.uploadPhotoAlbumFile(null, multipartFile, userId,activityId);
        if ("error".equals(jsonResult.getErrorCode())){
            //上传fast失败则删除之前插入的活动,
            sendActivity.deleteById(activityId);
            jsonResult.setErrorCode("500");
            jsonResult.setMsg("亲!活动创建失败...");
            return jsonResult;
        }
        return jsonResult;
    }

    @Transactional
    @Override
    public JsonResult saveActivityAlbum(TbActivityPhotoAlbum activityPhotoAlbum) {
        JsonResult<Object> jsonResult = new JsonResult<>();
        TbActivityPhotoAlbum save = IactivityPhotoAlbum.save(activityPhotoAlbum);
        if (save==null){
            sendActivity.deleteById(activityPhotoAlbum.getActivityId());
            jsonResult.setErrorCode("500");
            jsonResult.setMsg("亲!活动创建失败...");
            return jsonResult;
        }
        return jsonResult;
    }

    //查看该活动的相片
    @Override
    public JsonResult findAllPhotosByActId(String activityId) {
        JsonResult<Object> jsonResult = new JsonResult<>();
        List<TbPhotoLibrary> list = new ArrayList<>();
        List<TbPhotoLibrary> libraries = photoLibraryRepostitory.findTbPhotoLibrariesByActivityId(activityId);
        if (libraries.size()==0){
            //说明该活动创建时上传的是相册,去查相册编号
            TbActivityPhotoAlbum activityPhotoAlbum = IactivityPhotoAlbum.findTbActivityPhotoAlbumByActivityId(activityId);
            list = photoLibraryRepostitory.findTbPhotoLibrariesByAlbumIdAndOk(activityPhotoAlbum.getPhotoAlbumId(),1);
            jsonResult.setResult(list);
            return jsonResult;
        }
        for (TbPhotoLibrary pl:libraries){
            if (pl.getOk()==1){
                list.add(pl);
            }
        }
        jsonResult.setResult(list);
        return jsonResult;
    }


    //删除时还有几张相关的表没删除
    @Transactional
    @Override
    public JsonResult delActivity(String id) {
        JsonResult<Object> jsonResult = new JsonResult<>();
        sendActivity.deleteById(id);
        saveActivityUser.deleteTbActivityUserByActivityId(id);
        IactivityPhotoAlbum.deleteByActivityId(id);
        photoLibraryRepostitory.deleteByActivityId(id);
        return jsonResult;
    }

    @Transactional
    @Override
    public JsonResult updateActivity(TbActivity activity) {
        JsonResult<Object> jsonResult = new JsonResult<>();
       int i = sendActivity.updateActivity(
                activity.getActivityDetail(),
                activity.getActivityEndTime(),activity.getActivityBeginTime(),
                activity.getActivityMaxPeople(),activity.getActivityExpiredTime(),
               activity.getActivityBeginLocation(),activity.getActivityCover(),
               activity.getActivityTitle(),activity.getId()
        );
        if (i<=0){
            jsonResult.setMsg(SYSTEM_BUSY);
            jsonResult.setErrorCode("500");
            return jsonResult;
        }
        return jsonResult;
    }

    //ok
    @Override
    public JsonResult findActivityById(String id,String userId) {
        JsonResult<Object> jsonResult = new JsonResult<>();
        Object[] objects = new Object[2];
        TbActivity tbActivity = sendActivity.findById(id).get();
        objects[0]=tbActivity;
        TbActivityUser tbActivityUser = saveActivityUser.findTbActivityUserByActivityIdAndUserId(id, userId);
        if (tbActivityUser==null){
            //此人不在这个活动中,按钮不同显示
            objects[1]=0;
            jsonResult.setResult(objects);
            return jsonResult;
        }
        objects[1]=1;
        jsonResult.setResult(objects);
        return jsonResult;
    }

    @Transactional
    @Override
    public JsonResult updateActivityViewNum(String id) {
        JsonResult<Object> jsonResult = new JsonResult<>();
        int i = sendActivity.updateActivityViewNum(id);
        if (i<=0){
            jsonResult.setMsg(CAN_NOT_FIND);
            jsonResult.setErrorCode("500");
        }
        return jsonResult;
    }

    @Transactional
    @Override
    public JsonResult updateAvtivityOk(String id, long ok) {
        JsonResult jsonResult = new JsonResult();
        int i = sendActivity.updateAvtivityOk(id, ok);
        if (i<=0){
            jsonResult.setErrorCode("500");
            jsonResult.setMsg(SYSTEM_BUSY);
            return jsonResult;
        }
        return jsonResult;
    }

    @Override
    public JsonResult findAllActivity() {
        JsonResult jsonResult = new JsonResult();
        List<TbActivity> tbActivitiesByOk = sendActivity.findTbActivitiesByOk(1);
        if (tbActivitiesByOk.size()==0){
            jsonResult.setErrorCode("500");
            jsonResult.setMsg("亲!暂时没有活动...");
            return jsonResult;
        }
        jsonResult.setResult(tbActivitiesByOk);
        return jsonResult;
    }

    @Override
    public JsonResult getNewsActiviyList() {
        JsonResult jsonResult = new JsonResult();
        List<TbActivity> list = new ArrayList<>();
        List<TbActivity> activities = sendActivity.findTbActivitiesByOkAndActivityExpiredTimeAfterOrderByCreateTimeDesc(1, new Date());
        if (activities==null){
            jsonResult.setMsg("亲!暂无新活动");
            jsonResult.setErrorCode("500");
            return jsonResult;
        }
        for (TbActivity a:activities){
            //筛选出新活动里面人数没达到上限的
            long l = saveActivityUser.countByActivityId(a.getId());
            if (a.getActivityMaxPeople()<=l){
                jsonResult.setErrorCode("500");
                jsonResult.setMsg("亲!所有的新活动报名人数已达上限...");
                return jsonResult;
            }
            list.add(a);
        }
        jsonResult.setResult(list);
        return jsonResult;
    }

    @Override
    public JsonResult getCityActivityList(String activityLocation) {
        JsonResult jsonResult = new JsonResult();
        List<TbActivity> activities = sendActivity.findTbActivitiesByActivityLocationAndOk(activityLocation, 1);
        if (activities.size()==0){
            jsonResult.setErrorCode("500");
            jsonResult.setMsg("亲!目前还没有同城活动...");
            return jsonResult;
        }
        jsonResult.setResult(activities);
        return jsonResult;
    }

}
